using System;
using System.Web.UI.WebControls;
using TrainGame.Model;
using TrainGame.GlobalState;

namespace _1856.UI
{
	/// <summary>
	/// Summary description for StockMarket.
	/// </summary>
	public partial class StockMarket : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			FillTableWithStockMarketInfo ();
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		private void FillTableWithStockMarketInfo()
		{
			int			nCompanies;
			Game		game = Global.getGame (this);;
			TableCell	tableCell;

			nCompanies = 0;

			while (nCompanies < Game.NumberOfCompanies)
			{
				foreach (PublicCompany company in game.PublicCompanies)
				{
                    if (company.Open && company.CompanyAvailable && company.RunningOrder == nCompanies)
					{
						StockMarketPosition	marketPosition	= company.StockMarketPosition;

						int		nValue			= marketPosition.Value;			// 95
						int		nRunningOrder	= marketPosition.RunningOrder;	// 0, 1, ...
						string	sMarketPosition	= marketPosition.Location;		// A5
				
						tableCell = (TableCell) StockMarketTable.FindControl(sMarketPosition);
 
						if (tableCell.Text == marketPosition.Value.ToString())	// no company name here
							tableCell.Text = nValue.ToString() + "<BR>\t" + company.Name;
						else													// add another company name
                            tableCell.Text = tableCell.Text + "<BR>\t" + company.Name;
					}
				}
			
				nCompanies++;	// go to next round	
			}
		}


		protected void Back_Click(object sender, System.EventArgs e)
		{
			Game	game = Global.getGame (this);;

			if (game.ViewStockMarketFromStockRound)
				Response.Redirect("StockRound.aspx");
			else
				Response.Redirect("OperatingRound2.aspx");
		}
	}
}
